package tr.edu.yildiz.ce.dap.appagent.command.deployment.agent;

import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.AbstractAgentCommand;
import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.CommandConstants;
import tr.edu.yildiz.ce.dap.appagent.criteria.CriteriaConstants;
import tr.edu.yildiz.ce.dap.appagent.exception.AgentRetryException;
import tr.edu.yildiz.ce.dap.appagent.exception.BasicAgentException;
import tr.edu.yildiz.ce.dap.appagent.model.Criteria;
import tr.edu.yildiz.ce.dap.appagent.model.CriteriaList;
import tr.edu.yildiz.ce.dap.appagent.model.DapMessage;
import tr.edu.yildiz.ce.dap.appagent.process.base.AbstractProcess;

public class ListCommand extends AbstractAgentCommand implements CommandConstants,CriteriaConstants{
	
	String name ;
	String version;

	public ListCommand(AbstractProcess process,String name,String version) {
		super(process);
		this.name = name;
		this.version = version;
	}
	
	@Override
	protected void prepareRequest(DapMessage msg) throws BasicAgentException {
		msg.setCommand(CommandConstants.LIST);
		msg.setCriteriaList(new CriteriaList());
		
		Criteria nameCriteria = new Criteria();
		nameCriteria.setName(NAME.APP_NAME);
		
		if (name == null )
			throw new BasicAgentException("List command can't be used without name parameter : app-agent -n bundle_name ");
		
		if (name.contains("%"))
			nameCriteria.setOperator(OPERATOR.LIKE);
		else 
			nameCriteria.setOperator(OPERATOR.EQUALS);
		nameCriteria.setValue(name);
		msg.getCriteriaList().getCriteria().add(nameCriteria);
		
		if (version != null ){
			Criteria versionCriteria = new Criteria();
			versionCriteria.setName(NAME.APP_VERSION);
			if (name.contains("<") )
				versionCriteria.setOperator(OPERATOR.LESS_THAN);
			else if (name.contains(">") )
				versionCriteria.setOperator(OPERATOR.GREATER_THAN);
			else
				versionCriteria.setOperator(OPERATOR.EQUALS);
			versionCriteria.setValue(version);
			msg.getCriteriaList().getCriteria().add(versionCriteria);
		}
	}

	@Override
	protected void processResponse(DapMessage responseMsg)
			throws BasicAgentException, AgentRetryException {
		
		
	}

	@Override
	public DapMessage rollback(DapMessage msg) throws BasicAgentException {
		// TODO Auto-generated method stub
		return null;
	}

}
